//
//  WShopTableViewCell.m
//  Go!
//
//  Created by 凌風 on 16/7/12.
//  Copyright © 2016年 凌风. All rights reserved.
//

#import "WShopTableViewCell.h"
#import "UIImageView+ImageWithURL.h"
#import "WShopModel.h"

@interface WShopTableViewCell ()

@property (nonatomic, strong) UIImageView *myImageView;
@property (nonatomic, strong) UILabel_Font *titleLabel;
@property (nonatomic, strong) UILabel_Font *priceLabel;
@property (nonatomic, strong) UIButton *storBtn;
@property (nonatomic, strong) UILabel_Font *wordLabel;
@property (nonatomic, strong) UIView *separatorLine;
@end

@implementation WShopTableViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initSubViews];
    }
    return self;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initSubViews];
    }
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)initSubViews {
    [self addSubview:self.myImageView];
    [self addSubview:self.titleLabel];
    [self addSubview:self.priceLabel];
    [self addSubview:self.storBtn];
    [self addSubview:self.wordLabel];
    [self addSubview:self.separatorLine];
}

- (void)layoutSubviews {
    
    self.myImageView.frame = CGRectMake(Size_W(8), Size_H(8), CGRectGetHeight(self.bounds) - Size_H(16), CGRectGetHeight(self.bounds) - Size_H(16));

    self.titleLabel.frame = CGRectMake(CGRectGetMaxX(self.myImageView.frame) + Size_W(8), CGRectGetMinY(self.myImageView.frame), ScreenW - CGRectGetMaxX(self.myImageView.frame) - Size_W(8) - Size_W(40), Size_H(40));
    self.priceLabel.frame = CGRectMake(CGRectGetMaxX(self.myImageView.frame) + Size_W(8), CGRectGetMaxY(self.myImageView.frame) - Size_H(30), Size_W(100), Size_H(30));
    [self.priceLabel sizeToFit];
    
    self.wordLabel.frame = CGRectMake(CGRectGetMaxX(self.priceLabel.frame) + 2, CGRectGetMinY(self.priceLabel.frame), Size_W(44), Size_H(44));
    [self.wordLabel sizeToFit];
    
    self.storBtn.frame = CGRectMake(ScreenW - Size_W(40) - Size_W(10), Size_H(10), Size_W(40), Size_H(40));
    
    self.separatorLine.frame = CGRectMake(CGRectGetMinX(self.myImageView.frame), CGRectGetHeight(self.bounds) - 1, ScreenW - CGRectGetMinX(self.myImageView.frame), 1);
    
}

#pragma mark -- 监听事件回调方法
- (void)respondsToStorBtn:(UIButton *)sender {
    sender.selected = !sender.selected;
}

#pragma mark -- Setter
- (void)setModel:(WShopModel *)model {
    if (_model != model) {
        _model = model;
    }
    
    [self.myImageView setImageWithURL:model.imageURL placeHolderImage:[UIImage imageNamed:@"景点郊游.png"]];
    self.titleLabel.text = model.title;
    self.priceLabel.text = model.price;
    
}

#pragma mark -- Getter
- (UIImageView *)myImageView {
    if (!_myImageView) {
        _myImageView = [[UIImageView alloc] init];
    }
    
    return _myImageView;
}

- (UILabel_Font *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel_Font alloc] init];
        _titleLabel.text = @"旅游";
        _titleLabel.font = [UIFont boldSystemFontOfSize:Font(20)];
        
    }
    
    return _titleLabel;
}

- (UILabel_Font *)priceLabel {
    if (!_priceLabel) {
        _priceLabel = [[UILabel_Font alloc] init];
        _priceLabel.text = @"￥100";
        _priceLabel.textColor = [UIColor redColor];
    }
    
    return _priceLabel;
}

- (UIButton *)storBtn {
    if (!_storBtn) {
        _storBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [_storBtn.titleLabel setFont:[UIFont fontWithName:@"iconfont" size:Font(30)]];
        [_storBtn setTitle:@"\U0000e647" forState:UIControlStateNormal];
        [_storBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [_storBtn setTitle:@"\U0000e60f" forState:UIControlStateSelected];
        [_storBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
        
        [_storBtn addTarget:self action:@selector(respondsToStorBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _storBtn;
}

- (UILabel_Font *)wordLabel {
    if (!_wordLabel) {
        _wordLabel = [[UILabel_Font alloc] init];
        _wordLabel.text = @"起";
        _wordLabel.textColor = [UIColor grayColor];
    }
    
    return _wordLabel;
}

- (UIView *)separatorLine {
    if (!_separatorLine) {
        _separatorLine = [[UIView alloc] init];
        _separatorLine.backgroundColor = [UIColor grayColor];
    }
    
    return _separatorLine;
}

@end
